package com.lk2.core.config
{
	import com.lk2.core.events.LanguageEvent;
	
	import mx.collections.ArrayCollection;
	import mx.resources.IResourceManager;
	import mx.resources.ResourceBundle;
	import mx.resources.ResourceManager;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	
	public class LocaleObj extends AbsConfigObj
	{
		private var _configObj:RemoteConfigObj;
		private var _resourceManager:IResourceManager;
		private var _locales:ArrayCollection;
		private var _locale:String;
		private var _usedLocales:ArrayCollection;
		
		public function LocaleObj(configObj:RemoteConfigObj)
		{
			super(null);
			_configObj = configObj;
			
		/*** Retrieves locales info and build Locale ArrayCollection ***/
			var localesArray:Array = _configObj.getString('locales').split(',');
			var localToken:Array;
			_usedLocales = new ArrayCollection();
			_locales = new ArrayCollection();
			for each (var i:String in localesArray){
				localToken = i.split(' ');
				_locales.addItem({name: localToken[0], value: localToken[1]});
			}
			_locale = _locales[0].value;		
			this.loadLocaleFile();	
		}
		
		private function loadLocaleFile():void{
			super.configFile = _configObj.getString("localePath")+"/"+"flex_"+_locale+".cfm";
			var configService:HTTPService = new HTTPService();
			configService.url = super.configFile;
			configService.resultFormat="e4x";
			configService.method = "post";
			configService.addEventListener(ResultEvent.RESULT, configObjReady);
			configService.addEventListener(FaultEvent.FAULT,configObjError);
			configService.send();
		}

		
		private function configObjReady(event:ResultEvent):void{
			this.configXML = event.result as XML;
			setResourceManager();
			dispatchEvent(new LanguageEvent(LanguageEvent.LANGUAGE_CHANGED, this));
		}
		
		private function setResourceManager():void{
			_resourceManager = ResourceManager.getInstance();
			//_resourceManager.removeResourceBundlesForLocale(_locale);

			/*** Inserts Locales in localeChain ***/
			var chain:Array = new Array();
			var rb:ResourceBundle;
			rb = new ResourceBundle(_locale,_configObj.getString('app_uid'));
			var resources:XMLList = this.configXML.param;
			for each (var res:XML in resources){
				rb.content[res.@name] = res;
			}
			_resourceManager.addResourceBundle(rb);
			_resourceManager.update();
			_usedLocales.addItem(_locale);
			_resourceManager.localeChain = [_locale];
		}
		
		private function configObjError(event:FaultEvent):void{
		}
		
		public function changeLocale(newLocale:String):void{
			_locale = newLocale;
			if(_usedLocales.getItemIndex(newLocale) < 0){
				this.loadLocaleFile();
			}else{
				_resourceManager.localeChain = [_locale];
				dispatchEvent(new LanguageEvent(LanguageEvent.LANGUAGE_CHANGED, this));
			}
		}
		
		public function getNameOfDays():ArrayCollection{
			return new ArrayCollection(this.getString('day_names').split(','));
		}
		
		public function getDayName(dayNumber:Number):String{
			return this._resourceManager.getString(this.bundleName,'day_names').split(',')[dayNumber];
		}
		
		public function get locales():ArrayCollection{
			return this._locales;
		}

		public function get currentLocaleID():String{
			trace(_locale.substr(0,2));
			return this._locale.substr(0,2);
		}		

		public function getNameOfMonths():ArrayCollection{
			return  new ArrayCollection(this.getString('month_names').split('.'));
		}
		
		public function getMonthName(monthNumber:Number):String{
			return this._resourceManager.getString(this.bundleName,'month_names').split(',')[monthNumber];
		}
		
		[Bindable]
		public function get bundleName():String{
			return _configObj.getString("app_uid");
		}
		public function set bundleName(name:String){
			throw new Error("Read Only Property");
		}
		

	}
}